package io.renren.modules.demo.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.demo.dto.ChildThreeData;
import io.renren.modules.demo.dto.DhGodsInfoDTO;
import io.renren.modules.demo.excel.DhGodsInfoExcel;
import io.renren.modules.demo.service.DhGodsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@RestController
@RequestMapping("demo/dhgodsinfo")
@Api(tags = "")
public class DhGodsInfoController {
    @Autowired
    private DhGodsInfoService dhGodsInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")})
    @RequiresPermissions("demo:dhgodsinfo:page")
    public Result<PageData<DhGodsInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<DhGodsInfoDTO> page = dhGodsInfoService.page(params);

        return new Result<PageData<DhGodsInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("demo:dhgodsinfo:info")
    public Result<DhGodsInfoDTO> get(@PathVariable("id") Long id) {
        DhGodsInfoDTO data = dhGodsInfoService.get(id);

        return new Result<DhGodsInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("demo:dhgodsinfo:save")
    public Result save(@RequestBody DhGodsInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dhGodsInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("demo:dhgodsinfo:update")
    public Result update(@RequestBody DhGodsInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dhGodsInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("demo:dhgodsinfo:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        dhGodsInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("demo:dhgodsinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<DhGodsInfoDTO> list = dhGodsInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, DhGodsInfoExcel.class);
    }

    @PostMapping("/rent")
    @ApiOperation("物品租赁")
    @LogOperation("物品租赁")
    public Result rent(@RequestBody Map<String, Object> params) {
        Integer id = Integer.valueOf(String.valueOf(params.get("id")));
        Long userId = Long.valueOf(String.valueOf(params.get("userId")));
        double rentD = Double.parseDouble(String.valueOf(params.get("rent")));
        BigDecimal rent = BigDecimal.valueOf(rentD);
        dhGodsInfoService.rent(id, userId, rent);
        return new Result();
    }

    @PostMapping("/sell")
    @ApiOperation("物品出售")
    @LogOperation("物品出售")
    public Result sell(@RequestBody Map<String, Object> params) {
        Integer id = Integer.valueOf(String.valueOf(params.get("id")));
        Long userId = Long.valueOf(String.valueOf(params.get("userId")));
        double sellPriceD = Double.parseDouble(String.valueOf(params.get("sellPrice")));
        BigDecimal sellPrice = BigDecimal.valueOf(sellPriceD);
        dhGodsInfoService.sell(id, userId, sellPrice);
        return new Result();
    }

    @GetMapping("/getGodsName/{godsId}")
    @ApiOperation("获取物品名称")
    @LogOperation("获取物品名称")
    public Result<DhGodsInfoDTO> getGodsName(@PathVariable("godsId") Integer godsId) {
        String godsName = dhGodsInfoService.getGodsName(godsId);
        DhGodsInfoDTO data = new DhGodsInfoDTO();
        data.setGodsName(godsName);
        return new Result<DhGodsInfoDTO>().ok(data);
    }

    @GetMapping("/getGodsDataListByGodsName")
    @ApiOperation("通过运动物品名称查找指定物品")
    @LogOperation("通过运动物品名称查找指定物品")
    public Result<List<DhGodsInfoDTO>> getGodsDataListByGodsName(@RequestParam("godsName") String godsName) {
        List<DhGodsInfoDTO> data = dhGodsInfoService.getGodsDataListByGodsName(godsName);
        return new Result<List<DhGodsInfoDTO>>().ok(data);
    }

    @GetMapping("/getChildThreeData")
    @ApiOperation("获取商品名称、库存数量")
    @LogOperation("获取商品名称、库存数量")
    public Result<List<ChildThreeData>> getChildThreeData() {
        List<ChildThreeData> data = dhGodsInfoService.getChildThreeData();
        return new Result<List<ChildThreeData>>().ok(data);
    }

    @PostMapping("/newGods")
    @ApiOperation("上架新商品")
    @LogOperation("上架新商品")
    public Result<Integer> newGods(@RequestBody Map<String, Object> params) {
        Integer id = Integer.valueOf(String.valueOf(params.get("id")));
        String godsName = String.valueOf(params.get("godsName"));
        Integer amount = Integer.valueOf(String.valueOf(params.get("amount")));
        double rentD = Double.parseDouble(String.valueOf(params.get("rent")));
        BigDecimal rent = BigDecimal.valueOf(rentD);
        double sellPriceD = Double.parseDouble(String.valueOf(params.get("sellPrice")));
        BigDecimal sellPrice = BigDecimal.valueOf(sellPriceD);
        Integer data = dhGodsInfoService.newGods(id, godsName, amount, rent, sellPrice);
        return new Result<Integer>().ok(data);
    }

    @PostMapping("/addGods")
    @ApiOperation("补充物品库存")
    @LogOperation("补充物品库存")
    public Result<Integer> addGods(@RequestBody Map<String, Object> params) {
        Integer id = Integer.valueOf(String.valueOf(params.get("id")));
        Integer amount = Integer.valueOf(String.valueOf(params.get("amount")));
        Integer data = dhGodsInfoService.addGods(id, amount);
        return new Result<Integer>().ok(data);
    }
}